layui.config({
    base: config.base + 'layui-admin/'
}).extend({
    index: 'lib/index'
    , cron: 'cron/cron'
}).use(['index', 'cron'], function () {
    const $ = layui.$;
    const admin = layui.admin;
    const form = layui.form;
    const layer = layui.layer;
    const table = layui.table;
    const cron = layui.cron;
    const fn = {
        dump: function (item) {
            $('#id').val(0);
            $('#className').removeClass('layui-disabled');
            if (item) {
                $('#className').addClass('layui-disabled');
                form.val('quartz-form', item);//表单赋值
            } else {
                $('#quartz-form')[0].reset();//清空表单
            }
        }
        ,//查询
        query: function () {
            tableRender.reload({
                where: {
                    jobStatus: $('#jobStatus').val()
                }
                , page: {
                    curr: 1
                }
            });
        }
        //打开编辑窗口
        , edit: function (item) {
            fn.dump(item);
            admin.popup({
                type: 1
                , title: (item) ? '定时任务编辑' : '定时任务添加'
                , area: '100'
                , content: $('#quartz-form')
                , btn: ['确定', '取消']
                , success: function () {
                    cron.render({
                        elem: '#cronExpression'
                        , run: '../../admin/quartz/run.json'
                    });
                }
                , yes: function (index) {
                    //绑定用户编辑数据提交
                    form.on('submit(quartz-submit)', function (data) {
                        fn.save(data.field, function () {
                            layer.close(index);
                        });
                    });
                    $('#quartz-submit').trigger('click');
                }
            });
        }
        //删除
        , del: function (item) {
            layer.confirm('是否删除【' + item.jobName + '】定时任务？', {
                title: '删除'
                , icon: 3
                , closeBtn: 0
                , skin: 'layui-layer-admin'
            }, function (index) {
                layer.close(index);
                admin.req({
                    url: '../../admin/quartz/delete.json'
                    , type: 'delete'
                    , data: {id: item.id}
                    , done: function (data) {
                        tableRender.reload();
                    }
                });
            });
        }
        , resume: function (item) {
            layer.confirm('是否恢复【' + item.jobName + '】定时任务？', {
                title: '恢复'
                , icon: 3
                , closeBtn: 0
                , skin: 'layui-layer-admin'
            }, function (index) {
                layer.close(index);
                admin.req({
                    url: '../../admin/quartz/resume.json'
                    , data: {id: item.id}
                    , done: function (data) {
                        tableRender.reload();
                    }
                });
            });
        }
        , pause: function (item) {
            layer.confirm('是否暂停【' + item.jobName + '】定时任务？', {
                title: '暂停'
                , icon: 3
                , closeBtn: 0
                , skin: 'layui-layer-admin'
            }, function (index) {
                layer.close(index);
                admin.req({
                    url: '../../admin/quartz/pause.json'
                    , data: {id: item.id}
                    , done: function (data) {
                        tableRender.reload();
                    }
                });
            });
        }
        //保存（POST：添加；PUT：修改）
        , save: function (data, fn) {
            admin.req({
                url: '../../admin/quartz/' + ((data.id === '0') ? 'save.json' : 'edit.json')
                , contentType: "application/json;charset=UTF-8"
                , type: (data.id === '0') ? 'post' : 'put'
                , data: JSON.stringify(data)
                , done: function (data) {
                    tableRender.reload();
                    typeof fn === 'function' && fn();
                }
            });
        }
        , status: function () {
            admin.req({
                url: '../../admin/quartz/status.json'
                , done: function (data) {
                    $('#jobStatus').append(fn.option('', ''));
                    layui.each(data.data, function (index, item) {
                        $('#jobStatus').append(fn.option(item, item));
                    });
                    form.render("select");
                }
            });
        }
        , option: function (text, value) {
            const option = new Option();
            option.text = text;
            option.value = value;
            option.disabled = value === 0;
            return option;
        }
        , load: function () {
            fn.status();
        }
    };
    const tableRender = table.render({
        id: 'quartz-table'
        , elem: '#quartz-table'
        , toolbar: '#quartz-toolbar'
        , url: '../../admin/quartz/list.json'
        , page: true
        , response: {
            statusName: 'code'
            , statusCode: 0
            , msgName: 'message'
            , countName: 'total'
            , dataName: 'data'
        }
        , cols: [
            [
                {type: 'radio'}
                , {field: 'jobKey', title: '任务编码', hide: true}
                , {field: 'jobName', title: '任务名称', width: 200, templet: '#job-name-tpl'}
                , {field: 'className', title: '任务类名'}
                , {field: 'cronExpression', title: '任务表达式'}
                , {field: 'jobStatus', title: '任务状态', width: 110, templet: '#job-status-tpl'}
                , {field: 'jobExplain', title: '任务说明', hide: true}
                , {field: 'createUser', title: '创建用户', width: 90, hide: true}
                , {field: 'updateUser', title: '修改用户', width: 100, hide: true}
                , {field: 'createTime', title: '创建时间', width: 120, hide: true}
                , {field: 'updateTime', title: '修改时间', width: 120, hide: true}
                , {title: '操作', align: 'center', toolbar: '#quartz-tool', width: 140}
            ]
        ]
    });
    //绑定工具栏事件
    table.on('toolbar(quartz-table)', function (obj) {
        const items = table.checkStatus('quartz-table').data;
        console.log(items);
        if (obj.event === 'add-toolbar') {
            fn.edit();
        } else if (obj.event === 'edit-toolbar' || obj.event === 'del-toolbar') {
            if (items.length <= 0) {
                layer.msg("请选择一条纪录！");
            } else {
                if (obj.event === 'edit-toolbar') {
                    fn.edit(items[0]);
                } else if (obj.event === 'del-toolbar') {
                    fn.del(items[0]);
                }
            }
        } else if (obj.event === 'refresh-toolbar') {
            tableRender.reload();
        }
    });
    //绑定表格数据行操作事件
    table.on('tool(quartz-table)', function (obj) {
        const item = obj.data;
        if (obj.event === 'resume-tool') {
            fn.resume(item);
        } else if (obj.event === 'pause-tool') {
            fn.pause(item);
        }
    });
    fn.load();
    $('#query-field').on('click', function () {
        fn.query();
    });
})